/**
 * Created by tomi on 1/30/15.
 */
var Response = require('../util/ResponseUtil');
var ResponseCode = require('../util/ResponseNumber');
var User = require('../model/User');

var LoginValidation = function (req, res, next) {
    var token = req.get('X-USER-TOKEN');

    console.log("token : %s", token);

    if (token === null || token === undefined) {
        Response.ERROR(res, ResponseCode.no_access);
        return;
    }

    User.findByToken(token, function (err, user) {
        if (user === null || user === undefined) {
            Response.ERROR(res, ResponseCode.token_not_correct);
            return;
        }

        req.session.loginUser = user;
        next();
    });
};


module.exports = LoginValidation;